package com.cet.algorithm.test.字符串;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: cet-practice
 * @description: 最长不含重复元素连续子串长度
 * @author: 陈恩涛
 * @create: 2023-05-20 14:49
 **/
public class LC003 {

    public int lengthOfLongestSubstring(String s) {

        // 滑动窗口左端
        int left = 0;

        // 滑动窗口右端
        int length = s.length();

        int result = 0;

        // 维护不重复子串
        Set<Character> set = new HashSet<>();

        for (int j = 0; j < length; j++) {

            char c = s.charAt(j);

            while (set.contains(c)) {
                set.remove(s.charAt(left));
                left++;
            }

            set.add(c);

            result = Math.max(result, set.size());
        }

        return result;
    }
}

class Client003{
    public static void main(String[] args) {
        LC003 lc003 = new LC003();
        System.out.println(lc003.lengthOfLongestSubstring("abcabcbb"));
        System.out.println(lc003.lengthOfLongestSubstring("bbbbb"));
        System.out.println(lc003.lengthOfLongestSubstring("pwwkew"));
    }
}
